import { AfterViewChecked, Component, ElementRef, EventEmitter, Injector, Output, ViewChild } from '@angular/core';
import { AppConsts } from '@shared/AppConsts';
import { AppComponentBase } from '@shared/common/app-component-base';
import { PagedPaperDto, PagedPaperInput, WorkFlowQueryServiceProxy, WorkFlowQueryType} from '@shared/service-proxies/service-proxies';
import { ModalDirective } from 'ngx-bootstrap/modal';
import { map as _map, filter as _filter } from 'lodash-es';
import { finalize } from 'rxjs/operators';
import { Table } from 'primeng/table';
import { Paginator } from 'primeng/paginator';
import { LazyLoadEvent } from 'primeng/api';
import { WorkflowDetailComponent } from '../workflow/workflow-detail.component';
import { IBackWorkFlowListData } from '@app/shared/common/workflow/workflow-common-interface';
import { AppModalComponentBase } from '@shared/common/app-modal-component-base';

@Component({
    selector: 'dashboard-morelist',
    templateUrl: './dashboard.morelist.component.html'

})
export class DashboardMorelistModalComponent extends AppModalComponentBase {

    @ViewChild('morelistModal', { static: true }) modal: ModalDirective;
    @ViewChild('dataTable', { static: true }) dataTable: Table;
    @ViewChild('paginator', { static: true }) paginator: Paginator;
    @ViewChild('workFlowDetail', { static: true }) workflowDetailModal: WorkflowDetailComponent;

    @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();
    @Output() backtoList:EventEmitter<any> = new EventEmitter<any>();
    wfQueryType:WorkFlowQueryType
    modalTitle:string
    constructor(
        injector: Injector,
        private  _WorkFlowQueryServiceProxy:WorkFlowQueryServiceProxy
    ) {
        super(injector);
    }

    show(type: WorkFlowQueryType,title:string): void {
        this.modalExpandOb.resetStates();
        this.wfQueryType = type;
        this.modalTitle = title;
        this.modal.show();
        this.getListData()
    }

    getListData(event?: LazyLoadEvent) {
        if (this.primengTableHelper.shouldResetPaging(event)) {
            if (this.primengTableHelper.TryChangePage(this.paginator, 0)) {
                return;
            }
        }
        let listInput = new PagedPaperInput();
        listInput.wfQueryType = this.wfQueryType;
        listInput.sorting = this.primengTableHelper.getSorting(this.dataTable);
        listInput.maxResultCount = this.primengTableHelper.getMaxResultCount(this.paginator, event);
        listInput.skipCount = this.primengTableHelper.getSkipCount(this.paginator, event);
 
        this._WorkFlowQueryServiceProxy.getPagedPapers(listInput).pipe(finalize(() => this.primengTableHelper.hideLoadingIndicator()))
        .subscribe(result => {
            this.primengTableHelper.totalRecordsCount = result.totalCount;
            this.primengTableHelper.records = result.items;
            this.primengTableHelper.hideLoadingIndicator();
        });
    }

    onShown(): void {
        
    }

    goDetailModel(item: PagedPaperDto){
        this.workflowDetailModal.show({ paperId: item.paperId});
    }

    backToList(item: IBackWorkFlowListData){
        if (item && item.isReload) {
            this.reloadPage();
        }
    }
    reloadPage() {
        if (this.primengTableHelper.TryChangePage(this.paginator, this.paginator.getPage())) {
            return;
        }

        this.getListData(null);
    }

    close(): void {
        this.backtoList.emit(null);
        this.modal.hide();
    }
    
}
